package md.mru.frontend.beans;

import md.mru.backend.core.dtos.*;
import org.primefaces.context.RequestContext;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "skillsDataBean")
@SessionScoped
public class SkillsDataBean extends BackingBean {

    private static final long serialVersionUID = -8689440254784999383L;

    private List<EducationDto> educations = new ArrayList<>();
    private List<EducationStudyDto> educationStudies = new ArrayList<>();
    private List<EducationSpecialityDto> educationSpecialities = new ArrayList<>();
    private List<EducationCategoryDto> educationCategories = new ArrayList<>();
    private List<ModernLanguageDto> modernLanguages = new ArrayList<>();
    private List<LanguageDto> languages = new ArrayList<>();
    private List<KnowledgeLevelDto> knowledgeLevels = new ArrayList<>();

    public void deleteEducation(EducationDto e) {
        this.educations.remove(e);
    }

    public void addNewEducation(String editRowId) {
        EducationDto e = new EducationDto();
        educations.add(e);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ed-formId", "edData", editRowId));
    }

    public void deleteModernLanguage(ModernLanguageDto ml) {
        this.modernLanguages.remove(ml);
    }

    public void addNewModernLanguage(String editRowId) {
        ModernLanguageDto ml = new ModernLanguageDto();
        modernLanguages.add(ml);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ml-formId", "mlData", editRowId));
    }

    public List<EducationDto> getEducations() {
        return educations;
    }

    public void setEducations(List<EducationDto> educations) {
        this.educations = educations;
    }

    public List<EducationStudyDto> getEducationStudies() {
        if (educationStudies.isEmpty()) {
            educationStudies = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                EducationStudyDto et = new EducationStudyDto();
                et.setId(i);
                et.setName("Education type" + i);
                educationStudies.add(et);
                getObjectMappingConverter().mapObjectList(educationStudies);
            }

        }
        return educationStudies;
    }

    public void setEducationStudies(List<EducationStudyDto> educationStudies) {
        this.educationStudies = educationStudies;
    }

    public List<EducationSpecialityDto> getEducationSpecialities() {
        if (educationSpecialities.isEmpty()) {
            educationSpecialities = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                EducationSpecialityDto es = new EducationSpecialityDto();
                es.setId(i);
                es.setName("Speciality_" + i);
                educationSpecialities.add(es);
                getObjectMappingConverter().mapObjectList(educationSpecialities);
            }

        }
        return educationSpecialities;
    }

    public void setEducationSpecialities(List<EducationSpecialityDto> educationSpecialities) {
        this.educationSpecialities = educationSpecialities;
    }

    public List<EducationCategoryDto> getEducationCategories() {
        if (educationCategories.isEmpty()) {
            educationCategories = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                EducationCategoryDto ec = new EducationCategoryDto();
                ec.setId(i);
                ec.setName("Category_" + i);
                educationCategories.add(ec);
                getObjectMappingConverter().mapObjectList(educationCategories);
            }

        }
        return educationCategories;
    }

    public void setEducationCategories(List<EducationCategoryDto> educationCategories) {
        this.educationCategories = educationCategories;
    }

    public List<ModernLanguageDto> getModernLanguages() {
        return modernLanguages;
    }

    public void setModernLanguages(List<ModernLanguageDto> modernLanguages) {
        this.modernLanguages = modernLanguages;
    }

    public List<LanguageDto> getLanguages() {
        if (languages.isEmpty()) {
            languages = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                LanguageDto l = new LanguageDto();
                l.setId(i);
                l.setName("Language_" + i);
                languages.add(l);
                getObjectMappingConverter().mapObjectList(languages);
            }
        }
        return languages;
    }

    public void setLanguages(List<LanguageDto> languages) {
        this.languages = languages;
    }

    public List<KnowledgeLevelDto> getKnowledgeLevels() {
        if (knowledgeLevels.isEmpty()) {
            knowledgeLevels = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                KnowledgeLevelDto kl = new KnowledgeLevelDto();
                kl.setId(i);
                kl.setName("Knowledge_level_" + i);
                knowledgeLevels.add(kl);
                getObjectMappingConverter().mapObjectList(knowledgeLevels);
            }
        }
        return knowledgeLevels;
    }

    public void setKnowledgeLevels(List<KnowledgeLevelDto> knowledgeLevels) {
        this.knowledgeLevels = knowledgeLevels;
    }
}
